/******************************************************************************
Macroeconomc Analysis
Written by John Iselin - 03.13.2018

This program is designed to regress the wilshire 5000 index to create a 
predicted index for prior years. 
******************************************************************************/


*** Set up
capture log close
clear matrix
clear all
set more off


* Name of project
local proj = "macro_sp500"

* Date
local date = "03_23"


* Filepath
local mypath "D:\Sloan-recessions-project"

cd "`mypath'"

log using `proj'_`date', replace text

** Load data

import excel "new_macro_data_10_9.xlsx", sheet("Data") firstrow

** Destring vars

destring *, replace


** Variable Cleaning and Labels

rename natural_rate_unemployment_long_c unempl_natrate_long
rename natural_rate_unemployment_short_ unempl_natrate_short


label variable nominal_gdp_bea "Nominal Gross Domestic Product"
label variable real_gdp_bea "Real Gross Domestic Product, 2017 Dollars"
label variable pop_16plus_cps "Population Over the Age of 15"
label variable hh_employment_cps "Household Employment"
label variable potential_gdp_cbo "Nominal Potential GDP"
label variable real_potential_gdp_cbo "Real Potential GDP, 2017 Dollars"
label variable potential_laborforce_cbo "Potential Labor Force"
label variable unempl_natrate_long "Natural Rate of Unemployment, Long-Term"
label variable unempl_natrate_short "Natural Rate of Unemployment, Short-Term"
label variable potential_employment_lt "Potential Employment, based on LT Natural Unempl. Rate"
label variable potential_employment_st "Potential Employment, based on ST Natural Unempl. Rate"
label variable wilshire_5000 "Wilshire 5000 Index"
label variable qgdp_def "GDP Deflator"
label variable sp500 "SP-500 Average Closing Price"
label variable real_sp500 "Real SP-500 Average Closing Price"
label variable nominal_gdp_baseline "Baseline Nominal GDP (CY)"
label variable hh_employment_baseline "Employment, Civilian, 16 Years or Older (Household Survey)"
label variable payroll_employment "Total Nonfarm Payrolls"
label variable real_gdp_baseline "Baseline Real GDP (CY)"
label variable real_potential_gdp_baseline "Baseline Real Potential GDP"
label variable nominal_potential_gdp_baseline "Baseline Nominal Potential GDP"


*replace nominal_gdp_bea = nominal_gdp_bea/1000000000



** Time variable

tsset year

** Generate GDP Gap, the percent difference between potential GDP and real GDP
gen pct_gdp_gap = (real_gdp_bea- real_potential_gdp_cbo)/ real_potential_gdp_cbo
label variable pct_gdp_gap "Percent Difference Between Actual and Potential GDP"
gen del_pct_gdp_gap = D1.pct_gdp_gap
label variable del_pct_gdp_gap "Change in percent GDP gap"

gen pct_gdp_gap_squared = pct_gdp_gap^2
label variable pct_gdp_gap_squared "Percent GDP gap squared"

** Generate employment gap, the percent difference between actual and potential employment
gen pct_employment_gap = (hh_employment_cps - potential_employment_lt)/potential_employment_lt
label variable pct_gdp_gap "Percent Difference Between Actual and Potential GDP"




** Log of SP500

gen log_real_sp500 = log(real_sp500)
label variable log_real_sp500 "Natural Log of Real SP-500 Average Closing Price"
gen log_sp500 = log(sp500)
label variable log_sp500 "Natural Log of SP-500 Average Closing Price"

** Long-term productivity effect series
gen low_productivity_factor = 1 if year<2018
replace low_productivity_factor = l1.low_productivity_factor*.995 if year>2017
gen high_productivity_factor = 1 if year<2018
replace high_productivity_factor = l1.high_productivity_factor*1.005 if year>2017


des
sum

**regress gdp gap or change in gdp gap on lagged values
reg pct_gdp_gap L1.pct_gdp_gap
estat dwatson  
arima pct_gdp_gap, arima(1,0,0) noconst
arima pct_gdp_gap, arima(1,0,0)
*estat dwatson
estat acplot, lags(50)

predict pred_pct_gdp_gap
gen residuals = pct_gdp_gap-pred_pct_gdp_gap

*disp r(sd)


*histogram residuals, normal
*twoway (line del_pct_gdp_gap pred_del_pct_gdp_gap year)

gen new_pct_gdp_gap = pct_gdp_gap if year < 2018 
*left in to create history

** Regress the employment gap on current and lagged values of the GDP gap
*reg pct_employment_gap pct_gdp_gap L.pct_gdp_gap L2.pct_gdp_gap L3.pct_gdp_gap
prais pct_employment_gap pct_gdp_gap L.pct_gdp_gap L2.pct_gdp_gap 
!estat dwatson
predict pred_employment_gap
gen empl_residuals = pct_employment_gap - pred_employment_gap
gen adj_empl_residuals = empl_residuals - e(rho)*l1.empl_residuals
*histogram adj_empl_residuals, normal
summ adj_empl_residuals
*twoway (line pct_employment_gap pred_employment_gap year)


** Regress the Percent Change in the SP 500 Index on year and the change in 
**	the GDP Gap


prais log_sp500 year f1.pct_gdp_gap
*estat dwatson
predict pred_log_sp500
gen sp_residuals = log_sp500 - pred_log_sp500
gen adj_sp_residuals = sp_residuals - e(rho)*l1.sp_residuals
histogram adj_sp_residuals, normal
summ adj_sp_residuals, detail
*twoway (line log_sp500 pred_log_sp500 year)
gen new_log_sp500 = log_sp500 if year < 2018
 

 
 *export excel year pct_gdp_gap pct_employment_gap sp500 new_pct_gdp_gap new_pct_employment_gap new_sp500 potential_sp500 using test_data, replace
 
save macro_data_4_2_19.dta, replace
 
*clear

log close
 
 
 
 
